import 'dart:convert';
import 'package:ladder_cash/app/key_mix.dart';
import 'package:ladder_cash/generated/l10n.dart';
import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:ladder_cash/app/app.dart';
import 'package:ladder_cash/app/com/http/http_request.dart';
import 'package:ladder_cash/app/com/http/Res.dart';
import 'package:ladder_cash/app/com/model/login_user_info_model.dart';

class UserService {

  Future<LoginUserInfoModel?> login({phone, password, code}) async {
    Res r = await HttpRequest().postRes(
        '/service/login/sms-code-login'.hx, {'username'.hx: phone, 'smsCode'.hx: code},
        loadingText: S.current.loading);
    if (r.success) {
      LoginUserInfoModel userInfoModel =LoginUserInfoModel();
      userInfoModel.username=r.data['username'.hx];
      userInfoModel.sessionId=r.data['sessionId'.hx];
      App.userInfoModel = userInfoModel;
      SpUtils().setStorage("token", userInfoModel.sessionId);
      SpUtils()
          .setStorage("loginUserInfo", json.encode(userInfoModel.toJson()));

      App.token = userInfoModel.sessionId;
      return userInfoModel;
    } else {
      ToastUtils.toast(r.msg);
      return null;
    }
  }

}
